<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from '../utils/mixins/resize.js'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100%'
        },
        opinionData: {
            type: Array,
            default() {
                return [];
            },
        },
    },
    data() {
        return {
            chart: null,
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart(this.opinionData)

        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {

        initChart(data) {
            this.chart = echarts.init(this.$el, 'macarons')
            this.chart.setOption({
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}:{c} ({d}%)'
                },
                legend: {
                    bottom: '0',
                    left: 'center'
                },
                grid: {
                    containLabel: true
                },
                grid: {
                    left: '0%', //默认10%
                    right: '0%', //默认10%
                    bottom: '1%', //默认60
                    top: '5%',
                    containLabel: true
                    //grid区域是否包含坐标轴的刻度标签
                },
                series: [
                    {
                        type: 'pie',
                        radius: ['10%', '70%'],
                        
                        labelLine: {
                            show: true
                        },
                        data: [
                            { value: 1000, name: '未丢失设备', itemStyle: { color: '#5470c6' } },
                            { value: 10, name: '丢失设备', itemStyle: { color: '#91cc75' } },
                        ]
                    }
                ]
            })

            this.chart.resize()
        }
    }
}
</script>